package com.disability_federation.controller;

import com.disability_federation.dto.LoginUserDTO;
import com.disability_federation.mapper.MapperWorkingPersonnelList;
import com.disability_federation.pojo.WorkingPersonnelList;
import com.disability_federation.util.CreatJWT;
import com.disability_federation.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录接口
 */
@RestController
@CrossOrigin
public class Login {

    @Autowired
    private MapperWorkingPersonnelList mapperWorkingPersonnelList;

    /**
     * 登录接口
     */
    @RequestMapping("login")
    public Result login(@RequestBody LoginUserDTO loginUserDTO) {
        WorkingPersonnelList user = mapperWorkingPersonnelList.login(loginUserDTO.getUsername(), loginUserDTO.getPassword());
        if(user!=null){

            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", loginUserDTO.getUsername());
            claims.put("password", loginUserDTO.getPassword());

            String jwt = CreatJWT.generateJwt(claims);
            return new Result(1,"登录成功,已下发jwt令牌", jwt);
        }
        else{
            return new Result(0,"登录失败", "密码错误或用户不存在");
        }
    }
}
